package view;

import components.MButton;
import pri.swg.LineAZer;
import pri.swg.img.Bounds;
import pri.swg.img.Imager;
import resource.Resource;
import common.Singleton;

import javax.swing.*;
import java.awt.*;

/**
 * 菜单面板（主页）。负责菜单面板的视图
 *
 * @author 柴家琪
 *
 */
public class MenuPage extends JPanel {
    private static final long serialVersionUID = 1L;
    private Main m;
    private JPanel main = new JPanel();
    private Resource resource = Singleton.resource();

    private int numOfButtons = 6;
    private MButton[] buttons = new MButton[numOfButtons];
    private Color bg = Color.black;

    private static MenuPage menuPage;
    private MenuPage() {}
    static MenuPage getMenuPage() {
        if (menuPage == null){
            menuPage = new MenuPage();
            menuPage.m = Main.main;
            menuPage.uiSet();
            menuPage.add();
        }
        return menuPage;
    }

    private void uiSet() {
        setBounds(0, m.getHeight(), m.getWidth(), m.getHeight() - 30);
        setLayout(null);
//		setBackground(new Color(200,200,230));
        setBackground(bg);
    }

    /**
     * 添加组件和组件上的监听
     */
    private void add() {
        LineAZer line = new LineAZer(getWidth(), 100, LineAZer.Vertical),
                line2 = new LineAZer(getWidth(), 1, LineAZer.Horizontal),
                line3 = new LineAZer(getWidth(), 1, LineAZer.Horizontal);
        line.draw(new Color(37, 37, 37), Color.BLACK);
        line2.draw(Color.BLACK, Color.BLACK, Color.WHITE, Color.BLACK, Color.BLACK);
        line3.draw(Color.BLACK, Color.WHITE, Color.BLACK, Color.black);
        line.setLocation(0, 0);
        line2.setLocation(0, (getHeight() - 382) / 2);
        line3.setLocation(0, (getHeight() - 382) / 2 + 382 - 30);
        add(line);
//		add(line2);
        add(line3);
        main.setBackground(bg);
        main.setBounds((getWidth() - 618) / 2 - 200, (getHeight() - 382) / 2 - 30, 618, 382);
        main.setLayout(new GridLayout(numOfButtons, 1));
        add(main);

        buttons[0] = new MButton("编辑 xDoc", MButton.OPEN_EXISTING, new Color(100, 100, 255), "xdoc");
        buttons[1] = new MButton("选择 Word", MButton.OPEN, new Color(102, 204, 255), "word");
        buttons[2] = new MButton("保存 Xml 和 xDoc", MButton.SAVE, new Color(255, 100, 100), "xml");
        buttons[3] = new MButton("导入 Xml", MButton.IMPORT, new Color(255, 204, 102), "xml");
        buttons[4] = new MButton("生成 Excel", MButton.SAVE_EXCEL, new Color(100, 255, 100), "excel");
        buttons[5] = new MButton("导入 Excel", MButton.IMPORT_EXCEL, new Color(150, 255, 150), "excel");
        for (MButton button : buttons) {
            if (button != null)
                main.add(button);
        }
        setEnableOfButtonsOnMainPage();
        Imager image = new Imager(this, resource.urlOf("bdx_logo.png"),
                new Bounds(getWidth() - 255, getHeight() - 150, 255, 150));
        image.setDate(255, 150, 0, 0, 0, 0);
        add(image);

        Imager image2 = new Imager(this, resource.urlOf("school_logo.png"),
                new Bounds(getWidth() - 505, getHeight() - 165, 150, 150));
        image2.setDate(150, 150, 0, 0, 0, 0);
        add(image2);
    }

    /**
     * 重置，当关闭选项窗口时
     */
    public void setEnableOfButtonsOnMainPage() {
        boolean active = Singleton.handler().opened();
        for (int i = 2; i < numOfButtons; i++) {
            buttons[i].setVisible(active);
        }
    }
}
